package com.library.controller;

import com.library.bean.Book;
import com.library.bean.Lend;
import com.library.bean.PageInfo;
import com.library.bean.ReaderCard;
import com.library.service.BookService;
import com.library.service.LendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;

@Controller
@CrossOrigin
public class BookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private LendService lendService;


    @RequestMapping("/admin_books.html")
    public ModelAndView adminBooks(@RequestParam(defaultValue = "1") int currentPage) throws Exception{
        ArrayList<Book> books=bookService.findByCurrentPage(currentPage);

        ModelAndView modelAndView=new ModelAndView("admin_books");
        modelAndView.addObject("books",books);
        return modelAndView;
    }

    @RequestMapping("/admin_header.html")
    public ModelAndView adminheader(){
        ModelAndView modelAndView=new ModelAndView("admin_header");
        return modelAndView;

    }

    @RequestMapping("/findAll03")
    public ModelAndView findAll(@RequestParam(defaultValue = "1")int  currentPage) throws IOException {

        ModelAndView modelAndView=new ModelAndView("admin_books");
        //获取当前页信息
        PageInfo<Book> pageInfo=bookService.findByPage(currentPage);
        //当前页面下书籍数据信息
        ArrayList<Book> book=bookService.findByCurrentPage(currentPage);

        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.addObject("books",book);
        return modelAndView;

    }


    @RequestMapping("/admin_book_detail.html")
    public ModelAndView adminBookDetail(long bookId){
        Book book=bookService.getBookById(bookId);

        ModelAndView modelAndView=new ModelAndView("admin_book_detail");
        modelAndView.addObject("detail",book);
        return modelAndView;

    }

    @RequestMapping("/book_add.html")
    public ModelAndView bookAdd(){
        ModelAndView modelAndView=new ModelAndView("admin_book_add");
        return modelAndView;

    }

    @RequestMapping("/book_add_do.html")
    public String bookAddDo(Book book, RedirectAttributes redirectAttributes){

        if (bookService.addBook(book)){
            redirectAttributes.addFlashAttribute("succ","添加成功");
        }else {
            redirectAttributes.addFlashAttribute("succ","添加失败");
        }

        return "redirect:/admin_books.html";

    }

    @RequestMapping("/deletebook.html")
    public String delete(Long bookId,RedirectAttributes redirectAttributes){
        if (bookService.deleteBook(bookId)){
            redirectAttributes.addFlashAttribute("succ","删除成功");
        }else {
            redirectAttributes.addFlashAttribute("succ","删除失败");
        }

        return "redirect:/admin_books.html";
    }


    @RequestMapping("/updatebook.html")
    public ModelAndView updateBook(long bookId){
        Book book = bookService.getBookById(bookId);
        ModelAndView modelAndView=new ModelAndView("admin_book_edit");
        modelAndView.addObject("detail",book);
        return modelAndView;

    }

    @RequestMapping("/book_edit_do.html")
    public String bookEditDo(Book book,RedirectAttributes redirectAttributes){
        if (bookService.editBook(book)){
            redirectAttributes.addFlashAttribute("succ","修改成功");
        }else
            redirectAttributes.addFlashAttribute("succ","修改失败");

        return "redirect:/admin_books.html";

    }

    @RequestMapping({"querybook.html","/querybook"})
    public ModelAndView queryBook(String searchWord) throws IOException {
        if (bookService.matchBook(searchWord)){
            ArrayList<Book> books=bookService.qureyBook(searchWord);
            ModelAndView modelAndView=new ModelAndView("admin_books");
            modelAndView.addObject("books",books);
            return modelAndView;

        }else {
            return new ModelAndView("admin_books","error","没有匹配的图书");
        }

    }

    @RequestMapping("/reader_books.html")
    public ModelAndView readerBooks(HttpServletRequest request){
        ArrayList<Book> books=bookService.getAllBooks();

        ReaderCard readerCard= (ReaderCard) request.getSession().getAttribute("readercard");
        ArrayList<Lend> myAllLendList=lendService.myLendList(readerCard.getReader_id());
        ArrayList<Long> myLendList=new ArrayList<>();
        for (Lend lend:myAllLendList){
            //是否有已归还的书籍信息 若为null 说明还没有归还，将bookid存在集合中
            if (lend.getBack_date()==null){
                myLendList.add(lend.getBook_id());
            }

        }
        ModelAndView modelAndView=new ModelAndView("reader_books");
        modelAndView.addObject("books",books);
        modelAndView.addObject("myLendList",myLendList);
        return modelAndView;


    }

    @RequestMapping("/reader_header.html")
    public ModelAndView readerheader(){
        ModelAndView modelAndView=new ModelAndView("reader_header");
        return modelAndView;
    }

@RequestMapping("/reader_querybook_do.html")
    public ModelAndView readerQueryBookDo(String searchword) throws IOException {
    if (bookService.matchBook(searchword)){
        ArrayList<Book> books=bookService.qureyBook(searchword);
        ModelAndView modelAndView=new ModelAndView("reader_books");
        modelAndView.addObject("books",books);
        return modelAndView;

    }else {
        return new ModelAndView("reader_books","error","没有匹配的图书");
    }
}













}
